package com.uusee.crawler.pageprocessor.baike.mtime;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang.StringUtils;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

import com.uusee.crawler.framework.Processor;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.httpclient.SingleHttpConnectionManager;
import com.uusee.shipshape.bk.model.Baike;
import com.uusee.shipshape.bk.model.BaikeDataSource;

public class MoviePlotsPageProcessor extends Processor {
	private final String repReg;
	public MoviePlotsPageProcessor() {
		repReg = getReplaceReg();
		if ( StringUtils.isEmpty(repReg)) {
			throw new RuntimeException("时光网剧情解密正则表达式获取失败。");
		}
	}
	
	protected void innerProcess(CrawlURI crawlURI) {
		String summary = "";
		try {
			String crawlResult = crawlURI.getCrawlResult();
			Object model = crawlURI.getModel();
			int start = crawlResult.indexOf("\"paragraghs\": \" ")+"\"paragraghs\": \" ".length();
			int end = crawlResult.indexOf(",\"userId\":");
			String text = crawlResult.substring(start,end);
			
			//解密
			if ( StringUtils.isEmpty(repReg)) {	
				if (model instanceof Baike) {
					Baike baike = (Baike)model;
					baike.setSynopsis(summary);
				} else if (model instanceof BaikeDataSource) {
					BaikeDataSource baike = (BaikeDataSource)model;
					baike.setSynopsis(summary);
				}
				return;
			}
			
			text = text.replaceAll(repReg, "");
			text = text.replaceAll("\\\\t", "");
			text = text.replaceAll("\\\\", "");
			
			//System.out.println(text);
			String regx1 = "<div class=\"juqing\">.*?(\\S.*?)</div>";
			
			PatternCompiler compiler = new Perl5Compiler();
			Pattern pattern = compiler.compile(regx1, Perl5Compiler.CASE_INSENSITIVE_MASK);
			PatternMatcher matcher = new Perl5Matcher();
			PatternMatcherInput input = new PatternMatcherInput(text);
			StringBuffer sb = new StringBuffer();
			while (matcher.contains(input, pattern)) {
				MatchResult m = matcher.getMatch();
				String pp = m.group(1);
				pp = pp.replaceAll("<p>\\s+</p>", "");
				pp = pp.trim();
				sb.append(pp);
			}
			summary = sb.toString();
			if ( StringUtils.isNotEmpty(summary) ) {
				summary = summary.trim();
				summary = summary.replaceAll("<\\s*/p\\s*>\\s*?<\\s*p[^<]*?>", "::P_FLAG");
				summary = summary.replaceAll("<BR>", "::P_FLAG");
				summary = summary.replaceAll("<[^<]*?>", "");
				summary = summary.replaceAll("::P_FLAG", "<br/>");
				
				if (model instanceof Baike) {
					Baike baike = (Baike)model;
					baike.setSynopsis(summary);
				} else if (model instanceof BaikeDataSource) {
					BaikeDataSource baike = (BaikeDataSource)model;
					baike.setSynopsis(summary);
				}
				crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_SUCCESS);
			}
		} catch ( Exception e ) {
			e.printStackTrace();
			summary="";
		}
	}
	
	private String getReplaceReg() {
		long timeId = new Date().getTime();
		String cssUrl ="http://www.mtime.com/css/scramble.css?t="+timeId;
		
		String cssPage = "";
		HttpMethod method = null;
		InputStream in = null;
		try {
			HttpConnectionManager cm = new SingleHttpConnectionManager();

			HttpConnectionManagerParams hcmp = cm.getParams();
			hcmp.setConnectionTimeout(20000);
			hcmp.setStaleCheckingEnabled(true);
			hcmp.setTcpNoDelay(false);

			HttpClient http = new HttpClient(cm);
			HttpClientParams hcp = http.getParams();
			hcp.setSoTimeout(20000);
			hcp.setVersion(HttpVersion.HTTP_1_0);

			http.getParams().setParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, new Boolean(true));
			http.getParams().setParameter(HttpMethodParams.UNAMBIGUOUS_STATUS_LINE, new Boolean(false));
			http.getParams().setParameter(HttpMethodParams.STRICT_TRANSFER_ENCODING, new Boolean(false));
			http.getParams().setIntParameter(HttpMethodParams.STATUS_LINE_GARBAGE_LIMIT, 10);

			method = new GetMethod(cssUrl);
			method.addRequestHeader("Content-type", "text/css;charset=utf-8");
			method.addRequestHeader("User-Agent", "Mozilla/4.0");

			int statusCode = http.executeMethod(method);
			if (statusCode == 200) {
				StringBuilder sb = new StringBuilder();
				in = method.getResponseBodyAsStream();
				BufferedReader fIn = new BufferedReader(new InputStreamReader(in, "utf-8"));
				String l = fIn.readLine();
				while (l != null) {
					sb.append(l);
					l = fIn.readLine();
				}
				cssPage = sb.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (method != null) {
				method.releaseConnection();
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					
				}
				
			}
		}
		
		String regStr = "";
		StringBuffer sb = new StringBuffer();
		if ( StringUtils.isNotEmpty(cssPage)) {
			try {
				String regx1 = ".m_s([0-9]*?){display:none;}";
				PatternCompiler compiler = new Perl5Compiler();
				Pattern pattern = compiler.compile(regx1, Perl5Compiler.CASE_INSENSITIVE_MASK);
				PatternMatcher matcher = new Perl5Matcher();
				PatternMatcherInput input = new PatternMatcherInput(cssPage);
				while (matcher.contains(input, pattern)) {
					MatchResult m = matcher.getMatch();
					String pp = m.group(1);
					sb.append(pp);
					sb.append("|");
				}
				if (sb.length()>0) {
					sb.deleteCharAt(sb.length()-1);
				}
			}catch(Exception e) {
				regStr = "";
			}
			if (sb.length()>0) {
				regStr = "<span class=.\"m_s("+sb.toString()+").\">.*?</span>";
			}
		}
		return regStr;
	} 
}


